package com.qdaily.ui.ui;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import com.qdaily.supportlibrary.request.JsonData;
import com.qdaily.supportlibrary.util.NetUtil;
import com.qdaily.supportlibrary.util.QDJsonUtil;
import com.qdaily.supportlibrary.util.QLog;
import com.qdaily.ui.QDBaseActivity;
import com.qdaily.ui.R;
import com.qdaily.ui.data.EventModel.EventQDWebViewLoadComplete;
import com.qdaily.ui.net.NetConfigs;
import com.qdaily.ui.net.entity.ArticleInfoEntity;
import com.qdaily.ui.net.model.ArticleInfo;
import com.qdaily.ui.net.model.ArticleMeta;
import com.qdaily.ui.ui.fragment.ArticleDetailFragment;
import com.squareup.otto.Subscribe;

/**
 * 文章详情页面
 * Created with IntelliJ IDEA.
 * User: Felix
 * Date: 4/8/15
 * Time: 17:58
 * QQ:74104
 * Email:lovejiuwei@gmail.com
 */
public class ArticleDetailsActivity extends QDBaseActivity {
    public final static String KEY_DISPLAY_ARTICLE_DETAILS = "displayArticleDetails";
    public final static String KEY_DISPLAY_ARTICLE_ID = "displayArticleId";
    private ArticleMeta articleMeta;
    private ArticleInfo articleInfo;
    private ArticleDetailFragment fragment;
    private int articleId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        articleMeta = getIntent().getParcelableExtra(KEY_DISPLAY_ARTICLE_DETAILS);
        articleId = getIntent().getIntExtra(KEY_DISPLAY_ARTICLE_ID, -1);

        Uri mData = getIntent().getData();
        if (mData != null && mData.getHost() != null && (mData.getHost().equals(getString(R.string.ias_type_article)) || mData.getHost().equals(getString(R.string.ias_type_article)))) {
            articleId = Integer.valueOf(mData.getPath().replace("/", ""));
        }

        if (articleMeta == null && articleId == -1) {
            displayToast("访问出错，请稍后重试");
            QLog.e(QDTAG, "ArticleMeta传送失败");
            finish();
            return;
        }
        setContentView(R.layout.activity_article_details);
        if (savedInstanceState == null)
        {
            if (articleMeta != null)
            {
                articleId = articleMeta.getPost().getId();
                configFragment();
            }
            else
            {
                requestArticleInfo();
            }
        }
    }

    @Override
    public void onPause() {// 继承自Activity
        super.onPause();
        if (fragment != null && fragment.getmWebView() != null)
        {
            fragment.getmWebView().onPause();
        }
    }

    @Override
    public void onResume() {// 继承自Activity
        super.onResume();

        if (fragment != null && fragment.getmWebView() != null)
        {
            fragment.getmWebView().onResume();
        }
    }

    @Override
    protected void onDestroy() {
        if (fragment != null && fragment.getmWebView() != null)
        {
            fragment.getmWebView().loadData("", "text/html; charset=UTF-8", null);
            fragment.getmWebView().destroy();
        }
        super.onDestroy();
    }

    private void configFragment(){
        if (articleMeta!=null && this.findViewById(R.id.article_fragment_container) != null) {
            fragment = new ArticleDetailFragment();
            Bundle bundle = new Bundle();

            articleMeta.getPost().setPage_style(1); //需求需要，titlebar由web前端处理
            bundle.putParcelable(ArticleDetailFragment.KEY_ARTICLEMETA, articleMeta);
            bundle.putParcelable(ArticleDetailFragment.KEY_ARTICLEINFO, articleInfo);
            fragment.setArguments(bundle);
            getSupportFragmentManager().beginTransaction().add(R.id.article_fragment_container, fragment).commit();
        }
    }

    private void requestArticleInfo() {
        getRequestManager().requestArticleInfo(articleId);
    }

    @Override
    protected void onRequestCallBack(NetUtil.NetParams params, JsonData response) {
        super.onRequestCallBack(params, response);
        if (params instanceof NetConfigs.RequestArticleInfo)
        {
            ArticleInfoEntity articleInfoEntity = QDJsonUtil.Json2Object(response.toString(), ArticleInfoEntity.class);
            articleInfo = articleInfoEntity.getResponse();
            if (articleInfo != null && articleInfoEntity.getMeta().getStatus() == 200) {
                articleMeta = new ArticleMeta();
                articleMeta.setPost(articleInfo.getPost());
                configFragment();
            }
            else
            {
                displayToast(articleInfoEntity.getMeta().getMsg());
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                }, 1000);
            }
        }
    }

    @Override
    protected void onRequestErrorCallBack(NetUtil.NetParams params, Object response) {
        if (params instanceof NetConfigs.RequestArticleInfo)
        {
            displayToast("访问文章出错");
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    finish();
                }
            }, 1000);
        }
    }

    private boolean isReceiveWebViewLoadComplete = false;//可能会收到多次，只处理一次
    @Subscribe
    public void receiveWebViewLoadComplete(EventQDWebViewLoadComplete event)
    {
        if (fragment != null && event.getWebView() == fragment.getmWebView() && isReceiveWebViewLoadComplete == false)
        {
            isReceiveWebViewLoadComplete = true;
            QLog.w(QDTAG, "receiveWebViewLoadComplete");
        }
    }

}